﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for bzBranding
/// </summary>
public class bzShopAddress
{
		 private dbConnection _DB = null;
         private string _TableName = "bzShopAddress";
    private string _IDType = "_INT";
    private string _IDCol = "AddressID";
    private string _IDColCate = "CateID";
    private string _TableChild = "";
    private string _Field = "";
    public struct Enable
    {
        private static int _Enabled = 1;
        public static int Enabled { get { return _Enabled; } }

        private static int _Disabled = 0;
        public static int Disabled { get { return _Disabled; } }
    }
    public struct BrandPosition
    {
        private static string _Home = "1";
        public static string Home { get { return _Home; } }

        private static string _In = "0";
        public static string In { get { return _In; } }
    }
    public bzShopAddress()
    {
        _DB = new dbConnection();
    }
    public DataTable getID()
    {
        return _DB.getDataTable("bzShopAddress_searchInfo");

    }
    public DataTable getTable(string Key, string Enabled, string Page, string Limit, string Brand,string CateID,string CityID,string DisID)
    {
        SqlParameter[] p = new SqlParameter[8];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Page", Page);
        p[3] = new SqlParameter("@Limit", Limit);
        p[4] = new SqlParameter("@Shop", Brand);
        p[5] = new SqlParameter("@CateID", CateID);
        p[6] = new SqlParameter("@CityID", CityID);
        p[7] = new SqlParameter("@DisID", DisID);
        return _DB.getDataTable(_TableName + "_search", p);

    }
    public int changeHome(string ID)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@IDColActive", "Shop");
        return _DB.updateData("bz_updateRecordStatus", p);
    }
    public long getTotal(string Key, string Enabled, string Brand, string CateID, string CityID, string DisID)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Shop", Brand);
        p[3] = new SqlParameter("@CateID", CateID);
        p[4] = new SqlParameter("@CityID", CityID);
        p[5] = new SqlParameter("@DisID", DisID);
        return _DB.getTotalTable(_TableName + "_getTotal", p);
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    }
    public int saveOrder(string[] ID, string[] Order)
    {
        if (ID.Length != Order.Length) return 0;
        int count = 0;
        for (int i = 0; i < ID.Length; i++)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", ID[i]);
            p[3] = new SqlParameter("@Order", Order[i]);
            count += _DB.updateData("bz_saveOrder" + _IDType, p);
        }
        return count;

    }
    public int upOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@IDColParent", _IDColCate);
        return _DB.updateData("bz_upOrderParent" + _IDType, p);
    }
    public int downOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@IDColParent", _IDColCate);
        return _DB.updateData("bz_downOrderParent" + _IDType, p);
    }

    public int deleteRecord(string[] ID)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] ID, string Enabled)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", Enabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }
    public int insertRecord(string TitleVN, string TitleEN, string Address, string City, string Phone, string Fax, string Email,
        string Website, string DesVN, string DesEN, string PicThumb, string PicFull, string PostedDate, string ContentVN, string ContentEN, string Enabled, string CateID, string CityID, string DisID,string contactID)
    {
        SqlParameter[] p = new SqlParameter[20];
        p[0] = new SqlParameter("@TitleVN", TitleVN);
        p[1] = new SqlParameter("@TitleEN", TitleEN);
        p[2] = new SqlParameter("@Address", Address);
        p[3] = new SqlParameter("@City", City);
        p[4] = new SqlParameter("@Phone", Phone);
        p[5] = new SqlParameter("@Fax", Fax);
        p[6] = new SqlParameter("@Email", Email);
        p[7] = new SqlParameter("@Website", Website);
        p[8] = new SqlParameter("@DesVN", DesVN);
        p[9] = new SqlParameter("@DesEN", DesEN);
        p[10] = new SqlParameter("@PicThumb", PicThumb);
        p[11] = new SqlParameter("@PicFull", PicFull);
        p[12] = new SqlParameter("@PostedDate", PostedDate);
        p[13] = new SqlParameter("@ContentVN", ContentVN);
        p[14] = new SqlParameter("@ContentEN", ContentEN);
        p[15] = new SqlParameter("@Enabled",Enabled);
        p[16] = new SqlParameter("@CateID", CateID);
        p[17] = new SqlParameter("@CityID", CityID);
        p[18] = new SqlParameter("@DisID", DisID);
        p[19] = new SqlParameter("@ConTactID", contactID);
        return _DB.updateData(_TableName + "_insert", p);
    }
    public int updateRecord(string ID, string TitleVN, string TitleEN, string Address, string City, string Phone, string Fax, string Email,
        string Website, string DesVN, string DesEN, string PicThumb, string PicFull, string PostedDate, string ContentVN, string ContentEN, string Enabled, string CateID, string CityID, string DisID, string ConTactID)
    {
        SqlParameter[] p = new SqlParameter[21];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@TitleVN", TitleVN);
        p[2] = new SqlParameter("@TitleEN", TitleEN);
        p[3] = new SqlParameter("@Address", Address);
        p[4] = new SqlParameter("@City", City);
        p[5] = new SqlParameter("@Phone", Phone);
        p[6] = new SqlParameter("@Fax", Fax);
        p[7] = new SqlParameter("@Email", Email);
        p[8] = new SqlParameter("@Website", Website);
        p[9] = new SqlParameter("@DesVN", DesVN);
        p[10] = new SqlParameter("@DesEN", DesEN);
        p[11] = new SqlParameter("@PicThumb", PicThumb);
        p[12] = new SqlParameter("@PicFull", PicFull);
        p[13] = new SqlParameter("@PostedDate", PostedDate);
        p[14] = new SqlParameter("@ContentVN", ContentVN);
        p[15] = new SqlParameter("@ContentEN", ContentEN);
        p[16] = new SqlParameter("@Enabled", Enabled);
        p[17] = new SqlParameter("@CateID", CateID);
        p[18] = new SqlParameter("@CityID", CityID);
        p[19] = new SqlParameter("@DisID", DisID);
        p[20] = new SqlParameter("@ConTactID", ConTactID);
        return _DB.updateData(_TableName + "_update", p);
    }
    public DataTable getTableOther(string ID, string Page, string Limit, string Enabled, string Brand, string CateID, string CityID, string DisID)
    {
        SqlParameter[] p = new SqlParameter[8];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@Page", Page);
        p[2] = new SqlParameter("@Limit", Limit);
        p[3] = new SqlParameter("@Enabled", Enabled);
        p[4] = new SqlParameter("@Shop", Brand);
        p[5] = new SqlParameter("@CateID", CateID);
        p[6] = new SqlParameter("@CityID", CityID);
        p[7] = new SqlParameter("@DisID", DisID);
        return _DB.getDataTable(_TableName + "_other", p);
    }    
    public bool insertImage(string sFilePath, ref string sNameThumb, ref string sNameFull)
    {
        if (bzFile.Exist(sFilePath))
        {
            string arrRule = ".jpg.jpeg.bmp.png.gif";

            string[] strExt = sFilePath.Split('.');
            string ext = strExt[strExt.Length - 1].Trim().ToLower();
            if (arrRule.Contains("." + ext))
            {
                string sRan = DateTime.Now.ToString("ddmmyyyy_hhmmss");
                string sSuffix = "";
                string sSavePathThumb = HttpContext.Current.Server.MapPath("~") + "/Images/AddShop/";
                string sSavePathFull = HttpContext.Current.Server.MapPath("~") + "/Images/AddShop/";

                bzSiteOption op = new bzSiteOption(bzSiteOption.Group.Image, bzSiteOption.Note.AddShop);
                int iThumbWidth = 0,
                    iThumbHeight = 0,
                    iFullWidth = 0,
                    iFullHeight = 0;
                try
                {
                    iThumbWidth = Convert.ToInt32(op.SiteOption["ImageSize_Branding_WidthThumb"]);
                    iThumbHeight = Convert.ToInt32(op.SiteOption["ImageSize_Branding_HeightThumb"]);
                    iFullWidth = Convert.ToInt32(op.SiteOption["ImageSize_Branding_WidthFull"]);
                    iFullHeight = Convert.ToInt32(op.SiteOption["ImageSize_Branding_HeightFull"]);
                }
                catch
                {
                    iThumbWidth = 0;
                    iThumbHeight = 0;
                    iFullWidth = 0;
                    iFullHeight = 0;
                }

                sNameThumb = bzFile.Prefix + "_" + sSuffix + "thumb_" + sRan + "." + ext;
                sNameFull = bzFile.Prefix + "_" + sSuffix + "full_" + sRan + "." + ext;
                while (bzFile.Exist(sSavePathThumb + sNameThumb) || bzFile.Exist(sSavePathFull + sNameFull))
                {
                    int i = new Random().Next(1, 10);
                    sNameFull = sNameFull.Replace(".", i + ".");
                    sNameThumb = sNameThumb.Replace(".", i + ".");
                }
                bool flg = bzImage.resizeImage(sFilePath, sSavePathThumb + sNameThumb, sSavePathFull + sNameFull, iThumbWidth, iThumbHeight, iFullWidth, iFullHeight, ext.Equals("gif"));
                bzFile.delete(sFilePath);
                return flg;
            }

        }
        return false;
    }
    public int moveCate(string[] arrID, string CateID)
    {
        int count = 0;
        foreach (string str in arrID)
        {
            SqlParameter[] p = new SqlParameter[5];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@IDColParent", _IDColCate);
            p[4] = new SqlParameter("@IDValueParent", CateID);
            count += _DB.updateData("bz_updateCate" + _IDType, p);
        }
        return count;
    }
}
